package main

import (
	"fmt"
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/canvas"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/layout"
	"fyne.io/fyne/v2/storage"
	"fyne.io/fyne/v2/theme"
	"fyne.io/fyne/v2/widget"
	"log"
	"math/rand"
	"net/url"
	"os/exec"
	"strconv"
)

func foo() {
	myApp := app.New()
	myWindow := getAppTab(myApp)
	myWindow.Resize(fyne.NewSize(300, 400))
	myWindow.ShowAndRun()
}

func getSplit(myApp fyne.App) fyne.Window {
	myWindow := myApp.NewWindow("split Example")
	// 创建左右两个组件
	left := widget.NewLabel("左侧内容")
	right := widget.NewButton("右侧按钮", nil)

	// 水平分割（默认比例 50%:50%）
	split := container.NewHSplit(left, right)
	myWindow.SetContent(split)
	myWindow.Resize(fyne.NewSize(300, 400))
	return myWindow
}

func getAppTab(myApp fyne.App) fyne.Window {
	myWindow := myApp.NewWindow("table Example")
	// 创建标签页容器
	tabs := container.NewAppTabs(
		// 添加标签页
		container.NewTabItem("首页", widget.NewLabel("欢迎使用首页")),
		container.NewTabItem("设置", widget.NewEntry()),
		container.NewTabItem("关于", widget.NewButton("官网", nil)),
	)

	// 设置默认选中标签页（索引从0开始）
	tabs.SelectIndex(0)
	myWindow.SetContent(tabs)
	myWindow.Resize(fyne.NewSize(300, 400))
	return myWindow
}

func getTable(myApp fyne.App) fyne.Window {
	myWindow := myApp.NewWindow("table Example")

	// 定义数据源（二维切片）
	data := [][]string{
		{"Name", "Age", "Score"},
		{"Alice", "25", "89"},
		{"Bob", "30", "75"},
		{"Charlie", "28", "92"},
	}

	// 创建 Table 组件
	table := widget.NewTable(
		// 定义行数和列数
		func() (rows int, cols int) {
			return len(data), len(data[0])
		},
		// 创建单元格模板
		func() fyne.CanvasObject {
			return widget.NewLabel("template") // 模板对象
		},
		// 更新单元格内容
		func(id widget.TableCellID, cell fyne.CanvasObject) {
			cell.(*widget.Label).SetText(data[id.Row][id.Col])
		},
	)

	// 设置列宽（可选）
	table.SetColumnWidth(0, 150) // 第1列宽度150像素
	table.SetColumnWidth(1, 80)  // 第2列宽度80像素

	// 点击单元格事件
	table.OnSelected = func(id widget.TableCellID) {
		fmt.Printf("Clicked: Row=%d, Col=%d, Value=%s\n",
			id.Row, id.Col, data[id.Row][id.Col])
	}
	// 添加按钮更新数据
	updateBtn := widget.NewButton("Add Row", func() {
		newRow := []string{"New", strconv.Itoa(rand.Intn(100)), strconv.Itoa(rand.Intn(1000))}
		data = append(data, newRow)
		table.Refresh() // 刷新表格
	})
	content := container.New(
		layout.NewBorderLayout(nil, updateBtn, nil, nil),
		table,
		updateBtn)

	myWindow.SetContent(content)
	myWindow.Resize(fyne.NewSize(300, 400))
	return myWindow
}

func getList(myApp fyne.App) fyne.Window {
	myWindow := myApp.NewWindow("list Example")

	// 定义数据源
	data := []string{"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"}

	// 创建 List 组件
	list := widget.NewList(
		// 数据长度
		func() int {
			return len(data)
		},
		// 创建列表项模板
		func() fyne.CanvasObject {
			return widget.NewLabel("template") // 模板对象，用于复用
		},
		// 更新列表项内容
		func(i widget.ListItemID, o fyne.CanvasObject) {
			o.(*widget.Label).SetText(data[i] + "zzx") // 根据索引更新内容
		},
	)

	// 点击列表项事件
	list.OnSelected = func(id widget.ListItemID) {
		fmt.Println("Selected item:", data[id])
	}
	// 添加按钮触发数据更新
	button := widget.NewButton("Add Item", func() {
		data = append(data, "Item "+strconv.Itoa(len(data)+1))
		list.Refresh() // 必须调用 Refresh 更新显示
	})
	// 使用容器布局组合组件
	content := container.New(
		layout.NewBorderLayout(nil, button, nil, nil),
		list,
		button,
	)
	myWindow.SetContent(content)
	myWindow.Resize(fyne.NewSize(300, 400))
	return myWindow
}

func getRichText(myApp fyne.App) fyne.Window {
	myWindow := myApp.NewWindow("richText Example")

	richText := widget.NewRichTextFromMarkdown(`
		# Welcome to Fyne!

		This is a **bold** text and this is *italic* text.

		[Click here](https://example.com) to visit a link.
	`)
	myWindow.SetContent(container.NewVBox(richText))
	return myWindow
}

func getRadioGroup(myApp fyne.App) fyne.Window {
	myWindow := myApp.NewWindow("radioGroup Example")

	radioGroup := widget.NewRadioGroup([]string{"item1", "item2"}, func(s string) {
		fmt.Println(s)
	})
	myWindow.SetContent(container.NewVBox(radioGroup))
	return myWindow
}

// progress bar test
func getProgressBar(myApp fyne.App) fyne.Window {
	myWindow := myApp.NewWindow("progressBar Example")

	bar := widget.NewProgressBar()
	bar.TextFormatter = func() string {
		str := strconv.FormatFloat(bar.Value*100, 'f', 2, 64)
		return fmt.Sprintf("%s%%", str)
	}
	bar.SetValue(0.8128)
	myWindow.SetContent(container.NewVBox(bar))
	return myWindow
}

// icon demo
func getIcon(myApp fyne.App) fyne.Window {
	myWindow := myApp.NewWindow("icon Example")

	// 使用文件路径加载图标
	iconRe, err := fyne.LoadResourceFromPath("D:/Pictures/wallpaper-master/lufei.jpg")
	if err != nil {
		log.Fatal(err)
	}
	icon := widget.NewIcon(iconRe)
	myWindow.SetContent(container.NewVBox(icon))
	return myWindow
}

// hyperlink demo
func getHyperlink(myApp fyne.App) fyne.Window {
	myWindow := myApp.NewWindow("hyperlink Example")
	u, _ := url.Parse("https://www.baidu.com")
	hyperlink := widget.NewHyperlink("百度", u)
	myWindow.SetContent(container.NewVBox(hyperlink))
	return myWindow
}

// fileIcon demo
func getForm(myApp fyne.App) fyne.Window {
	myWindow := myApp.NewWindow("form Example")
	item1 := widget.NewFormItem("name", widget.NewEntry())
	form := widget.NewForm(item1)
	myWindow.SetContent(container.NewVBox(form))
	return myWindow
}

// fileIcon demo
func getFileIcon(myApp fyne.App) fyne.Window {
	myWindow := myApp.NewWindow("fileIcon Example")
	// 创建一个文件图标
	fileURI, err := storage.ParseURI("file:///D:/Documents/notes/e9.md")
	if err != nil {
		log.Fatal(err)
	}
	fileIcon := widget.NewFileIcon(fileURI)
	myWindow.SetContent(container.NewVBox(fileIcon))
	return myWindow
}

// entry demo
func getEntry(myApp fyne.App) fyne.Window {
	myWindow := myApp.NewWindow("Entry Example")
	entry := widget.NewEntry()
	entry.SetText("Hello World")
	entry.OnSubmitted = func(s string) {
		fmt.Println("Entry submitted:", s)
	}
	myWindow.SetContent(container.NewVBox(entry))
	return myWindow
}

// check demo
func getCheck(myApp fyne.App) fyne.Window {
	myWindow := myApp.NewWindow("Check Example")
	card := widget.NewCheck("Hello", func(b bool) {
		if b {
			fmt.Println("true")
		} else {
			fmt.Println("false")
		}
	})
	// card.SetImage(img)
	myWindow.SetContent(container.NewVBox(card))
	return myWindow
}

// card demo
func getCard(myApp fyne.App) fyne.Window {
	myWindow := myApp.NewWindow("Button Example")
	img := canvas.NewImageFromResource(theme.FyneLogo())
	img.FillMode = canvas.ImageFillContain
	img.SetMinSize(fyne.NewSize(100, 100))
	card := widget.NewCard("Hello", "world", img)
	// card.SetImage(img)
	myWindow.SetContent(container.NewVBox(card))
	return myWindow
}

// button demo
func getButtons(myApp fyne.App) fyne.Window {
	myWindow := myApp.NewWindow("Button Example")
	bt1 := widget.NewButtonWithIcon("Button 1", theme.ContentAddIcon(), func() {
		fmt.Println("Click bt1")
	})
	bt2 := widget.NewButtonWithIcon("Button 2", theme.ContentCopyIcon(), func() {
		fmt.Println("Click bt2")
	})
	bt3 := widget.NewButtonWithIcon("Open WT", theme.FolderOpenIcon(), func() {
		err := exec.Command("wt").Run()
		if err != nil {
			return
		}
	})
	myWindow.SetContent(container.NewVBox(bt1, bt2, bt3))
	return myWindow
}

// activity demo
// error
func getActivity(myApp fyne.App) fyne.Window {
	myWindow := myApp.NewWindow("Activity Indicator Example")
	activity := widget.NewActivity()
	myWindow.SetContent(container.NewVBox(activity))
	return myWindow
}

// widget according demo
func getAccordionDemo(myApp fyne.App) fyne.Window {
	myWindow := myApp.NewWindow("Container")
	accordion := widget.NewAccordion()
	for i := 0; i < 3; i++ {
		item := widget.AccordionItem{
			Title:  fmt.Sprintf("Item %d", i),
			Detail: widget.NewLabel(fmt.Sprintf("Item %d", i)),
		}
		accordion.Append(&item)
	}
	myWindow.SetContent(container.NewVBox(accordion))
	return myWindow
}
